package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.context.BaseContext;
import com.example.demo.dto.ElderlyServiceAddDTO;
import com.example.demo.dto.PageDTO;
import com.example.demo.pojo.ElderlyBasicOk;
import com.example.demo.pojo.ElderlyService;
import com.example.demo.service.ElderlyBasicOkService;
import com.example.demo.service.ElderlyServiceService;
import com.example.demo.mapper.ElderlyServiceMapper;
import com.example.demo.service.ServiceService;
import com.example.demo.utils.RedisIdWorker;
import com.example.demo.vo.ElderlyServiceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author yangqi
* @description 针对表【db_elderly_service】的数据库操作Service实现
* @createDate 2024-01-18 11:35:24
*/
@Service
public class ElderlyServiceServiceImpl extends ServiceImpl<ElderlyServiceMapper, ElderlyService>
    implements ElderlyServiceService {
    @Autowired
    private RedisIdWorker redisIdWorker;
    @Autowired
    private ElderlyBasicOkService elderlyBasicOkService;
    @Autowired
    private ServiceService serviceService;
    @Override
    @Transactional
    public void addElderlyService(ElderlyServiceAddDTO elderlyServiceAddDTO) {
        Long elderlyId = elderlyServiceAddDTO.getElderlyId();
        List<Long> servicesIds = elderlyServiceAddDTO.getServicesIds();
        int size = servicesIds.size();
        List<ElderlyService> services = new ArrayList<>();

        for (int i = 0; i < size; i++) {
            ElderlyService es = new ElderlyService();
            es.setCreateTime(LocalDateTime.now());
            es.setElderlyId(elderlyId);
            es.setServiceId(servicesIds.get(i));
            es.setId(redisIdWorker.nextId("service"));
            services.add(es);
        }

        saveBatch(services);

    }

    @Override
    @Transactional
    public List<ElderlyServiceVO> getElderlyService(Long id) {
        // TODO 这里先不分页查询了 之后想办法
        // 需要判断传来的用户是否数ru
        // 获取当前的全部子用户全部用户
        // 获取当前用户下的全部老人的id
        List<ElderlyBasicOk> elderlyList = elderlyBasicOkService.lambdaQuery().eq(ElderlyBasicOk::getUserId, id).list();
        List<Long> idList = elderlyList.stream()
                .map(ElderlyBasicOk::getId)
                .toList();
        // 根据这些id来查找老人相关的服务
        List<ElderlyServiceVO> list = new ArrayList<>();

        for (Long l : idList) {
            List<ElderlyService> elderlyServices = lambdaQuery().eq(ElderlyService::getElderlyId, l).list();
            List<Long> serviceIds = elderlyServices.stream()
                    .map(ElderlyService::getServiceId)
                    .toList();
            List<com.example.demo.pojo.Service> services =
                    serviceService.lambdaQuery().in(com.example.demo.pojo.Service::getId, serviceIds).list();
            list.add(ElderlyServiceVO.builder().serviceList(services)
                    .elderlyId(l).build());
        }

        return list;
    }
}




